﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MasoQna.Infrastructure
{
    public class RemoveWwwAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase req = filterContext.HttpContext.Request;
            HttpResponseBase res = filterContext.HttpContext.Response;


            string host = req.Url.Host.ToLower();
            if (host.StartsWith("www."))
            {
                var builder = new UriBuilder(req.Url)
                {
                    Host = host.Substring(4)
                };
                res.Redirect(builder.Uri.ToString());
            }

            base.OnActionExecuting(filterContext);
        }
    }
}